<?php
/**
 * ComProductsDetailDao.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/10/27- 11:11 上午
 */

namespace common\dao;


use common\exception\ValidatorException;
use common\library\BaseDao;
use common\models\com\ComProductsDetail;

class ComProductsDetailDao extends BaseDao
{


    /**
     * 写入产品详细表
     * @param array $data
     * @return ComProductsDetail
     * @throws ValidatorException
     */
    public function saveData(array $data):ComProductsDetail
    {

        $detail = ComProductsDetail::findOne(['product_id'=>$data['product_id']]);

        if(!$detail)
        {
            $detail = new ComProductsDetail();
        }

        $detail->load($data,'');

        if (!$detail->validate()) {
            throw new ValidatorException($detail->errorsToString());
        }

        if($detail->save())
        {
            return $detail;
        }else{
            throw new ValidatorException($detail->errorsToOneString());
        }

    }



}